<template>
  <div style="margin-top: 20px">
    <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input v-model="searchForm.keyword" placeholder="宠物名称"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.state" placeholder="审核状态">
          <el-option label="上架" value="1"></el-option>
          <el-option label="下架" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上架时间">
        <el-date-picker
            v-model="betweenTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="toQuery">查询</el-button>
        <el-button type="primary" @click="toAdd">新增</el-button>
        <el-button type="danger" @click="">批量删除</el-button>
        <el-button type="warning" @click="upDown(1)">上架</el-button>
        <el-button type="default" @click="">下架</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="tableData"
        border
        style="width: 100%;"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="resources"
          label="宠物图片">
        <template slot-scope="scope">
          <el-image :src="scope.row.resources"
                    :preview-src-list="[scope.row.resources]"></el-image>
        </template>
      </el-table-column>
      <el-table-column
          prop="name"
          label="宠物名称">
      </el-table-column>
      <el-table-column
          prop="state"
          label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0" type="info">下架</el-tag>
          <el-tag v-if="scope.row.state == 1" type="warning">上架</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄">
      </el-table-column>
      <el-table-column
          prop="saleprice"
          label="销售价格">
      </el-table-column>
      <el-table-column
          prop="costprice"
          label="成本价">
      </el-table-column>
      <el-table-column prop="petTypeName"
                       label="宠物类型">

      </el-table-column>
      <el-table-column
          prop="shopName"
          label="所属门店">
      </el-table-column>
      <el-table-column
          prop="username"
          label="宠物发布人">
      </el-table-column>
      <el-table-column
          prop="onsaletime"
          label="上架时间">
      </el-table-column>

      <el-table-column label="操作" width="80px">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="toEdit(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        style="margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchForm.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="searchForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>


    <el-dialog title="审核操作" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标题" label-width="100px">
          <el-input v-model="form.title" disabled></el-input>
        </el-form-item>
        <el-form-item label="审核备注" label-width="100px">
          <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="form.note"
              maxlength="200"
              show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="shenghe(1)">审核通过</el-button>
        <el-button @click="shenghe(2)">审核驳回</el-button>
      </div>
    </el-dialog>

    <el-dialog fullscreen title="编辑宠物信息" :visible.sync="dialogPetFormVisible">
      <el-form ref="form" :model="petForm" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="petForm.name"></el-input>
        </el-form-item>
        <el-form-item label="宠物图片">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:80/file/upload/"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
            <img v-if="petForm.resources" :src="petForm.resources" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="销售价">
          <el-input v-model="petForm.saleprice"></el-input>
        </el-form-item>
        <el-form-item label="促销价">
          <el-input v-model="petForm.chuxiaoPrice"></el-input>
        </el-form-item>
        <el-form-item label="服务备注">
          <el-input type="textarea" v-model="petForm.petDetail.intro"></el-input>
        </el-form-item>
        <el-form-item label="服务详情">
          <quill-editor
              :options="editorOption"
              v-model="petForm.petDetail.adoptNotice"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="savePet()">保存</el-button>
        <el-button @click="dialogPetFormVisible=false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {quillEditor} from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';

import quillConfig from '../../common/js/quill-config'

export default {
  name: "PetShengHe",
  components: { // 引入一个 自定义的components组件
    'quill-editor': quillEditor //QuillEditor  ---默认名称：quill-editor
  },
  data() {
    return {
      editorOption: quillConfig,
      betweenTime: null, //查询条件
      form: { //下面的3个属性，可省略
        id: '',//审核的数据的主键 ID
        title: '', //显示的标题
        note: '', //备注,
        state: null //审核状态
      },
      petForm: {
        resources:"",
        petDetail: {
          intro:"",
          adoptNotice:""
        }
      },//宠物编辑表单
      dialogPetFormVisible: false, //宠物编辑弹框
      dialogFormVisible: false, //宠物审核弹框
      total: 0, //总记录数
      multipleSelection: [],
      searchForm: {
        page: 1,
        pageSize: 10
      },
      tableData: []
    }
  },
  methods: {
    //
    upDown(state){
      //拿到的是数据的list  ids = [1,2]
      //let para = {};
      let ids = this.multipleSelection.map(item=> item.id)


      console.log(JSON.stringify(ids))
      this.$http.post("/pet/up",ids).then(res => {

      })
      console.log("调用上下架的方法")
    },
    savePet() {
      this.$http.post('/pet/save', this.petForm).then(res => {
        let {success, msg} = res.data;
        if (success) {
          this.dialogPetFormVisible = false;
          this.toQuery();
        }
        this.$message.info(msg);
      })
    },
    toAdd() {
      this.dialogPetFormVisible = true;
    },
    toEdit(row) {
      //根据主键ID查询宠物信息（带petDetail详情）
      //this.$http.get("/pet/" + id).then(res => {
        this.petForm = row;
        this.petForm.petDetail = {};
        this.dialogPetFormVisible = true;
      // })

    },
    handleAvatarSuccess(res, file) {
      //this.imageUrl = res.data.webUrl;
      //将图片的真实地址赋值给shop实体类中的logo属性
      //this.shop.shop.logo = res.data.url;
      //this.imageUrl = res.data.webUrl;
      this.petForm.resources = res.data.webUrl;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    shenghe(state) {
      this.form.state = state;
      this.$http.post('/searchmaster/shenghe/', this.form).then(res => {
        let {msg, success} = res.data;
        if (success) {
          this.dialogFormVisible = false;
          this.toQuery();
        } else {
          this.$message.error(msg);
        }
      })
    },
    toQuery() {
      if (this.betweenTime && this.betweenTime.length > 0) {//判断参数非空（!= '' && != underfind && != NAN）
        this.searchForm.betweenTime = this.betweenTime.join(",");
      } else {
        this.searchForm.betweenTime = ''
      }
      this.$http.post("/pet/findAll", this.searchForm).then(res => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      })
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.toQuery();
    },
    handleCurrentChange(val) {
      this.searchForm.page = val;
      this.toQuery();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  },
  mounted() {
    this.toQuery();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
